package com.woniuxy.main.handler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.main.pojo.Coach;
import com.woniuxy.main.pojo.Order;
import com.woniuxy.main.pojo.Student;
import com.woniuxy.main.pojo.Student_coach;
import com.woniuxy.main.pojo.Vi_coachOrder_studentInfo;
import com.woniuxy.main.service.CoachService;
import com.woniuxy.main.service.OrderService;
import com.woniuxy.main.service.StudentService;
import com.woniuxy.main.util.SessionUtil;

import lombok.Data;

@Data
@Controller
@RequestMapping("/coach")
public class CoachHandler {

	@Autowired
	private CoachService coachService;
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private OrderService orderService;
	
	/**
	 * 查询学员教练中间表信息
	 */
	@RequestMapping("/allContract")
	@ResponseBody
	public HashMap<String, Object> findAllContract(int currentUid){
		currentUid = (int) SessionUtil.getSession().getAttribute("uid");
		// 根据uid查询cid
		int cid = coachService.selectByUid(currentUid).getCid();
		
		//获取学员教练签约中间表信息
		List<Student_coach> student_coachs = coachService.findAllContract(cid);
		HashMap<String, Object> contracts = new HashMap<>();
		if (!student_coachs.isEmpty()) {
			System.out.println("------------测试-------------");
			//根据学员id查学员个人信息
			Student student = studentService.findStudentBySid(student_coachs.get(0).getSid());
			contracts.put("contract", student_coachs);
			contracts.put("student", student);
		}
		return contracts;
	}
	
	@RequestMapping("/showMyStudentList")
	@ResponseBody
	public Map<String,Object> showMyStudentList(int uid,int page){
		//查询当前教练的cid
		Coach coach=coachService.selectByUid(uid);
		//System.out.println("开始运行");
		int index = (page - 1) * 8;
		Map<String,Object> result=new HashMap<String,Object>();
		Map<String,Object> request=new HashMap<String,Object>();
		request.put("cid", coach.getCid());
		request.put("page", index);
		List<Vi_coachOrder_studentInfo> info=coachService.showMyStudentList(request);
		result.put("form", info);
		//查询总页码
		int totalCount=coachService.myStudentTotalCount(uid);
		result.put("totalCount", totalCount);
		int totalPage=totalCount % 8 == 0 ? totalCount / 8 : totalCount / 8 + 1;
		result.put("totalPage", totalPage);
		return result;
	}
	/**
	 * 检验是否有效请求
	 */
	@RequestMapping("/checkStatus")
	@ResponseBody
	public String checkStatus(String time){
		String result = "已过期";
		long lt=0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		try {
			lt = sdf.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long l = (System.currentTimeMillis()-lt)/1000/60;
		//6个小时内有效
		if (l<=360) {
			result = "有效申请";
		}
		return result;
	}
	/**
	 * 接受签约--修改状态
	 */
	@RequestMapping("/alterStatus_adopt")
	@ResponseBody
	public String alterStatus_adopt(int cid,int sid,String isRequest){
		String result = "签约失败，该申请已过期";
		if (isRequest.contains("有效")) {
			//操作student_coach表，更改状态
			Student_coach sc = new Student_coach();
			sc.setCid(cid);
			sc.setSid(sid);
			sc.setUpdateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			sc.setStatus("已接受");
			coachService.alterStatus(sc);
			
			//操作order表，新增一条订单信息，createDate、sid、cid
			Order order = new Order();
			order.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			order.setSid(sid);
			order.setCid(cid);
			orderService.insertOrder(order);
			
			result = "签约成功，你已接受此签约";
		}
		return result;
	}
	/**
	 * 暂不处理--修改状态
	 */
	@RequestMapping("/alterStatus_refuse")
	@ResponseBody
	public String alterStatus_refuse(int cid,int sid,String isRequest){
		String result = "处理失败,该申请已过期";
		if (isRequest.contains("有效")) {
			Student_coach sc = new Student_coach();
			sc.setCid(cid);
			sc.setSid(sid);
			sc.setUpdateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			sc.setStatus("已拒绝");
			//操作Student_coach表
			coachService.alterStatus(sc);
			result = "处理成功，你已拒绝此签约";
		}
		return result;
	}
	
	
}
